package alibaba.hashmap;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author ChangLiang
 * @date 2020/6/26
 */
@Slf4j
public class HashMapEndlessLoopTest {

    // 当有3个内容的时候 不会触发扩容 当为4个内容的时候 会触发扩容
    private static HashMap<Integer, String> map = new HashMap<>(2, 1.5f);

    @Test
    @DisplayName("test")
    public void test() throws InterruptedException {
        map.put(5, "C");
        map.put(7, "B");
        map.put(3, "A");
        new Thread(() -> {
            map.put(15, "D");
            log.info("map:{}", map);
        }, "my-thead-1").start();
        new Thread(() -> {
            map.put(1, "E");
            log.info("map:{}", map);
        }, "my-thead-2").start();
        Thread.currentThread().join();
    }

    @Test
    @DisplayName("test")
    public void test2() throws InterruptedException {
        map.put(5, "C");
        map.put(7, "B");
        map.put(3, "A");
        new Thread(() -> {
            map.put(15, "D");
            log.info("map:{}", map);
        }, "my-thead-1").start();
        TimeUnit.MILLISECONDS.sleep(10);
        new Thread(() -> {
            map.put(1, "E");
            log.info("map:{}", map);
        }, "my-thead-2").start();
        Thread.currentThread().join();
    }
}
